package com.itheima.youdianda.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.common.Result;
import com.itheima.youdianda.domain.YddAdvimg;
import com.itheima.youdianda.domain.YddAdvpos;
import com.itheima.youdianda.mapper.YddAdvposMapper;
import com.itheima.youdianda.service.YddAdvimgService;
import com.itheima.youdianda.mapper.YddAdvimgMapper;
import com.itheima.youdianda.service.YddAdvposService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author YUAN
 * @description 针对表【ydd_advimg】的数据库操作Service实现
 * @createDate 2022-09-06 18:36:39
 */
@Service
public class YddAdvimgServiceImpl extends ServiceImpl<YddAdvimgMapper, YddAdvimg>
        implements YddAdvimgService {


    @Autowired
    private YddAdvposMapper yddAdvposMapper;

    /**
     * 分页条件
     *
     * @param page
     * @param limit
     * @param advimgdesc
     * @param advimgpos
     * @return
     */
    @Override
    public Result pageByList(Integer page, Integer limit, String advimgdesc, String advimgpos) {
        Page ipage = new Page(page, limit);

        LambdaQueryWrapper<YddAdvimg> lqw = new LambdaQueryWrapper<>();
        lqw.like(StrUtil.isNotBlank(advimgdesc), YddAdvimg::getAdvimgdesc, advimgdesc);//模糊
        lqw.like(StrUtil.isNotBlank(advimgpos), YddAdvimg::getAdvimgpos, advimgpos);//模糊

        ipage = page(ipage, lqw);

        List<YddAdvimg> records = ipage.getRecords();
        records.stream().map(record -> {
            //advposname

            //YddAdvpos yddAdvpos = yddAdvposService.getById(record.getId());
            YddAdvpos yddAdvpos = yddAdvposMapper.selectById(record.getAdvimgpos());

            record.setAdvposname(yddAdvpos.getAdvposname());
            return record;
        }).collect(Collectors.toList());

        //用map集合处理下参数
        Map<String, Object> map = new HashMap<>();
        map.put("data", ipage.getRecords());
        map.put("count", ipage.getTotal());
        map.put("totalPages", ipage.getPages());
        map.put("pageSize", ipage.getSize());
        map.put("currentPage", ipage.getCurrent());

        return Result.ok(map);
    }


    /**
     * 广告图 -- 添加
     *
     * @param yddAdvimg
     * @return
     */
    @Override
    public void add(YddAdvimg yddAdvimg) {
        yddAdvimg.setDefaultData("1");
        save(yddAdvimg);
    }


}




